//
//  MWLanguageSwitchController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/30.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWLanguageSwitchController.h"
#import "MWTabBarController.h"
#import "MWXRTabBarController.h"

#import "MWRadioCell.h"

#import "HSFTabBarController.h"

#import "MWNavigationController.h"
#import "MWHomeController.h"
#import "ShowRoomViewController.h"
#import "MWShopController.h"
#import "MWTreeSeedController.h"
#import "MWApplicationController.h"
#import "MWMineController.h"

#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX kScreenWidth >=375.0f && kScreenHeight >=812.0f&& kIs_iphone
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))

@interface MWLanguageSwitchController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, assign) NSInteger selectPosition;

@end

@implementation MWLanguageSwitchController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"selectlanguage" defaultValue:@"选择语言"];
    
    _selectPosition = [[MWConfigManager manager].language intValue];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


- (void)loadLocalizableLanguage:(NSString *)language {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:language forKey:@"lang"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    
    [[MWNetworkManager manager] POST:kLocalizableConfigUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            [MWConfigManager manager].localizableDic = [dic objectForKey:@"data"];
            [[MWConfigManager manager] saveConfig];
            if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
                NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
                NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
                NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
                NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
                NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
                NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

                MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
                MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
                MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
                MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
                MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
                MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
                HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
                rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
                rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},
                @{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
                @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                            @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                          @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

                @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
                rootController.norColor = MWHexColor(@"#666666");
                rootController.selColor = MWHexColor(@"#333333");
                rootController.loadAll = YES;//是否一次全部加载
                rootController.canScroll = NO;
                rootController.tabBarHeight = kTabBarHeight;
                rootController.style = HSFTabBarStyle_baseline;
                rootController.baseline_color = MWHexColor(@"#333333");
                rootController.isHaveTopline = NO;
                [rootController setUp];
                UIApplication *application = [UIApplication sharedApplication];
                application.keyWindow.rootViewController = rootController;
                [MWUserManager manager].tbc = rootController;
            } else {
                [[MWConfigManager manager] setProductType:@"1"];
                MWXRTabBarController *controller = [[MWXRTabBarController alloc] init];
                UIApplication *application = [UIApplication sharedApplication];
                application.keyWindow.rootViewController = controller;
            }
            [MWLocalizableManager configCopywriter];
        }
        
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWRadioCell";
    MWRadioCell *radioCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!radioCell) {
        radioCell = [[MWRadioCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        radioCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    MWWeakSelf
    radioCell.block = ^() {
        weakSelf.selectPosition = indexPath.row;
        [weakSelf.tableView reloadData];
        
        if (indexPath.row == 0) {
//            [MWLocalizableManager setUserLanguage:@"zh-Hans"];
            [MWConfigManager manager].language = @"0";
            [weakSelf loadLocalizableLanguage:@"zh"];
            
        } else if (indexPath.row == 1) {
//            [MWLocalizableManager setUserLanguage:@"en"];
            [MWConfigManager manager].language = @"1";
            [weakSelf loadLocalizableLanguage:@"en"];
            
        } else {
//            [MWLocalizableManager setUserLanguage:@"en"];
            [MWConfigManager manager].language = @"2";
            [weakSelf loadLocalizableLanguage:@"ja"];
        }
    };
    
    if (_selectPosition == indexPath.row) {
        radioCell.checked = YES;
        
    } else {
         radioCell.checked = NO;
    }
    
    if (indexPath.row == 0) {
        radioCell.titleLB.text = [[MWConfigManager manager] objectForKey:@"chinese" defaultValue:@"中文"];
    } else if (indexPath.row == 1) {
        radioCell.titleLB.text = [[MWConfigManager manager] objectForKey:@"english" defaultValue:@"英语"];
    } else {
        radioCell.titleLB.text = [[MWConfigManager manager] objectForKey:@"japanese" defaultValue:@"日文"];
    }
    
    return radioCell;
}


@end
